package net.lab1024.sa.admin.module.flowable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.flowable.annotation.EnterpriseAndApplicationCodeProperty;
import net.lab1024.sa.admin.module.flowable.domain.FlowableModelPage;
import net.lab1024.sa.admin.module.flowable.page.TableDataInfo;
import net.lab1024.sa.admin.module.flowable.service.IFlowableModelPageService;
import net.lab1024.sa.base.common.code.ErrorCode;
import net.lab1024.sa.base.common.code.SystemErrorCode;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 建模页面绑定Controller
 *
 * @author ruoyi
 * @date 2023-12-25
 */
@RestController
@RequestMapping("/flowable/page")
public class FlowableModelPageController extends BaseController
{
    @Resource
    private IFlowableModelPageService flowableModelPageService;

    /**
     * 查询建模页面绑定列表
     */
    @SaCheckPermission("flowable:page:list")
    @GetMapping("/list")
    public ResponseDTO<TableDataInfo> list(FlowableModelPage flowableModelPage)
    {
        startPage();
        List<FlowableModelPage> list = flowableModelPageService.selectFlowableModelPageList(flowableModelPage);
        return ResponseDTO.ok(getDataTable(list));
    }

    /**
     * 导出建模页面绑定列表
     */
/*    @SaCheckPermission("flowable:page:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlowableModelPage flowableModelPage)
    {
        List<FlowableModelPage> list = flowableModelPageService.selectFlowableModelPageList(flowableModelPage);
        ExcelUtil<FlowableModelPage> util = new ExcelUtil<FlowableModelPage>(FlowableModelPage.class);
        util.exportExcel(response, list, "建模页面绑定数据");
    }*/

    /**
     * 获取建模页面绑定详细信息
     */
    @SaCheckPermission("flowable:page:query")
    @GetMapping(value = "/{id}")
    public ResponseDTO<FlowableModelPage> getInfo(@PathVariable("id") String id)
    {
        return ResponseDTO.ok(flowableModelPageService.selectFlowableModelPageById(id));
    }

    /**
     * 新增建模页面绑定
     */
    @SaCheckPermission("flowable:page:add")
    @PostMapping
    public ResponseDTO<Integer> add(@RequestBody FlowableModelPage flowableModelPage)
    {
        return ResponseDTO.ok(flowableModelPageService.insertFlowableModelPage(flowableModelPage));
    }

    /**
     * 修改建模页面绑定
     */
    @SaCheckPermission("flowable:page:edit")
    @PutMapping
    public ResponseDTO<Integer> edit(@RequestBody FlowableModelPage flowableModelPage)
    {
        return ResponseDTO.ok(flowableModelPageService.updateFlowableModelPage(flowableModelPage));
    }

    /**
     * 删除建模页面绑定
     */
    @SaCheckPermission("flowable:page:remove")
	@DeleteMapping("/{ids}")
    public ResponseDTO<Integer> remove(@PathVariable String[] ids)
    {
        return ResponseDTO.ok(flowableModelPageService.deleteFlowableModelPageByIds(ids));
    }
    /**
     * 建模页面绑定
     */
    @SaCheckPermission("flowable:page:bind")
    @PutMapping(value="/bind")
    public ResponseDTO<Integer> bind(@RequestBody FlowableModelPage flowableModelPage)
    {
        List<FlowableModelPage> list = flowableModelPageService.selectFlowableModelPageListByBind(flowableModelPage);
        if(CollectionUtils.isEmpty(list)){
            return ResponseDTO.ok(flowableModelPageService.insertFlowableModelPage(flowableModelPage));
        }else {
            flowableModelPage.setId(list.get(0).getId());
            return ResponseDTO.ok(flowableModelPageService.updateFlowableModelPage(flowableModelPage));
        }
    }
    /**
     * 建模页面单页面查询（根据模块，流程标识，页面名称查询)
     */
    @SaCheckPermission("flowable:page:findPage")
    @PostMapping(value="/findPage")
    public ResponseDTO<FlowableModelPage> findPage(@RequestBody FlowableModelPage flowableModelPage)
    {
        FlowableModelPage page = flowableModelPageService.selectFlowableModelPageSingle(flowableModelPage);
        return ResponseDTO.ok(page);
    }
    /**
     * 建模页面模块页面查询（按模块，流程标识查询)
     */
    @SaCheckPermission("flowable:page:findModulePage")
    @PostMapping(value="/findModulePage")
    public ResponseDTO<List<FlowableModelPage>> findModulePage(@RequestBody FlowableModelPage flowableModelPage)
    {
        List<FlowableModelPage> list = flowableModelPageService.selectFlowableModelPage(flowableModelPage);
        return ResponseDTO.ok(list);
    }
    /**
     * 建模页面解绑
     */
    @SaCheckPermission("flowable:page:unbind")
    @PutMapping(value="/unbind")
    public ResponseDTO<Integer> unbind(@RequestBody FlowableModelPage flowableModelPage)
    {
        List<FlowableModelPage> list = flowableModelPageService.selectFlowableModelPageListByBind(flowableModelPage);
        if(CollectionUtils.isEmpty(list)){
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR,"页面没找到!");
        }else {
            return ResponseDTO.ok(flowableModelPageService.deleteFlowableModelPageById(list.get(0).getId()));
        }
    }
}
